package com.bps.zzant.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.bps.zzant.model.Customer;
import com.bps.zzant.model.OrderGarment;
import com.bps.zzant.model.ToDo;


public class CustomerDAOImpl implements CustomerDAO {
	@Autowired
	private SessionFactory sessionFactory;
	
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}


	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	public List<Customer> getCustomersByName(String customerName) {
		System.out.print("---------CustomerDAOImpl.getCustomersByName:" + customerName);
		List<Customer> customerList = getSessionFactory().getCurrentSession().createCriteria(Customer.class).add(Restrictions.like("customerName", customerName)).list();
		return customerList;
	}
	
	public List<Customer> getAllCustomers() {
		System.out.print("---------CustomerDAOImpl.getAllCustomers:" );
		List<Customer> customerList = getSessionFactory().getCurrentSession().createCriteria(Customer.class).add(Restrictions.like("customerName", "%")).list();
		return customerList;
	}
	
	public Customer getCustomerByCustomerId(int customerId){
		System.out.print("---------CustomerDAOImpl.getCustomerByCustomerId:" +customerId);
		Customer customer = (Customer)getSessionFactory().getCurrentSession().get(Customer.class, new Integer(customerId));
		return customer;		
	}
	
	public List<Customer> getCustomersByPhoneOrFax(String getCustomersByPhoneOrFax) {
		System.out.print("------11111111 ---CustomerDAOImpl.getCustomersByPhone:" + getCustomersByPhoneOrFax);
		
		Criterion phoneCondition = Restrictions.like("phone1", getCustomersByPhoneOrFax);
		Criterion faxCondition = Restrictions.like("fax", getCustomersByPhoneOrFax);	
		Criterion completeCondition = Restrictions.disjunction().add(phoneCondition).add(faxCondition);
		
		List<Customer> customerList = getSessionFactory().getCurrentSession().createCriteria(Customer.class).add(completeCondition).list();
		
		
		//List<Customer> customerList = getSessionFactory().getCurrentSession().createCriteria(Customer.class).add(Restrictions.like("phone1", phone1)).list();
		return customerList;
	}
	
	public void saveCustomer(Customer customer){
		System.out.println("-------before customer is saved the customer id: " + customer.getId());
		getSessionFactory().getCurrentSession().save(customer);
		getSessionFactory().getCurrentSession().flush();
		System.out.println("-------after customer is saved the customer id: " + customer.getId());		
	}
	
	public void updateCustomer(Customer customer) {
		getSessionFactory().getCurrentSession().merge(customer);
		getSessionFactory().getCurrentSession().flush();		
	}
	
	
}
